package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import uteis.Constantes;
import model.ContaAgua;

public class ContaAguaJDBCCadastro implements ContaAguaCadastroDAO {

	Connection conexao;

	public ContaAguaJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(ContaAgua contaagua) {
		String sqlquery = "insert into conta_agua(CDCLIENTE, NUMHIDROMETRO, DIAVENCIMENTO, BOLATIVO)"
				+ " values(?,?,?,?);";
		PreparedStatement pstmt;
		try {
			pstmt = this.conexao.prepareStatement(sqlquery);
			pstmt.setString(1, contaagua.getCdcliente());
			pstmt.setString(2, contaagua.getNumhidrometro());
			pstmt.setString(3, contaagua.getDiavencimento());
			pstmt.setString(4, "S");
			pstmt.execute();

			contaagua.setNumhidrometro("");
			contaagua.setDiavencimento("");
			contaagua.setCdcliente("");

			return ("success");
		} catch (Exception e) {
			e.printStackTrace();
			return ("error");
		}
	}

	public boolean localizarBanco(String nhidrometro) {
		String sqlquery = "SELECT NUMHIDROMETRO FROM conta_agua WHERE (NUMHIDROMETRO like '"
				+ nhidrometro
				+ "' and CDCLIENTE = "
				+ Constantes.cdempresas
				+ ") ORDER BY NUMHIDROMETRO ASC;";
		try {
			String valor = "";
			java.sql.Statement stmt = conexao.createStatement();
			ResultSet rs = null;
			rs = stmt.executeQuery(sqlquery);
			while (rs.next()) {
				valor = rs.getString("NUMHIDROMETRO");
			}
			if (valor.equals("")) {
				return (false);
			} else {
				return (true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return (false);
		}
	}

	public void editar(ContaAgua contaagua) {
	}

	public void excluir(ContaAgua contaaguas) {
	}

	public List<ContaAgua> ListarContaAguaT() {
		return null;
	}

}
